package ex55.mm.flow.impound.quote.common.kit;

import java.io.IOException;
import java.net.ConnectException;

/**
 * 2位 类别 ，3-5 code ，6位 A 输入异常，B 服务器端异常 , C 服务内部错误
 */
public interface Warning {
    //通用的
    FriendlyException Common_Error = new FriendlyException(0x01000C, "common error");
    FriendlyException Illegal_Argument = new FriendlyException(0x01001A, "IllegalArgumentException");
    FriendlyException Server_Error = new FriendlyException(0x01002B, "server error");
    FriendlyException ServerBusy = new FriendlyException(0x01003B, "server busy please wait");
    FriendlyException Deprecated = new FriendlyException(0x01004A, "Deprecated");

    //
    FriendlyException Open_Signature_Error = new FriendlyException(0x20000A, "Signature Error");
    FriendlyException Open_Signature_Not_Found = new FriendlyException(0x20001A, "Open_Signature_Not_Found");

    static FriendlyException newServerError(String message) {
        return new FriendlyException(Server_Error.getCode(), message);
    }

    static FriendlyException newRequestError(String message) {
        return new FriendlyException(Illegal_Argument.getCode(), message);
    }

    static FriendlyException newRequestPathError() {
        return newRequestError("path error");
    }


    static FriendlyException newServerError(Throwable e) {
        return new FriendlyException(e);
    }

    static FriendlyException newCommonError(String message) {
        return new FriendlyException(Common_Error.getCode(), message);
    }

    static FriendlyException newFriendlyException(int code, String message) {
        return new FriendlyException(code, message);
    }

    static FriendlyException newCommonError(Throwable e) {
        if (e instanceof FriendlyException) {
            return ((FriendlyException) e);
        }
        return new FriendlyException(Common_Error.getCode(), e);
    }

    static boolean isConnectException(IOException e) {
        return e instanceof ConnectException;
    }

    static boolean isException(Exception e, FriendlyException fe) {
        if (e instanceof FriendlyException) {
            FriendlyException friendlyException = (FriendlyException) e;
            return friendlyException.getCode() == fe.getCode();
        }
        return false;
    }
}
